﻿using FPSO.HMI.Controls;
using FPSO.HMI.Utils;
using FPSO.Models.Constant;
using FPSO.Models.Domain;
using FPSO.Models.ViewModel.Controls;
using FPSO.Services;
using FPSO.Services.Impl;
using SafeLoad.NControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shell;
using System.Windows.Threading;

namespace FPSO.HMI.Views.Dialog
{
    /// <summary>
    /// CabinMonitorDialog.xaml 的交互逻辑
    /// </summary>
    public partial class TankMonitorDialog : Window
    {
        private DispatcherTimer timer;
		private List<string> tanklist = new List<string>();
		private double maxLevel = 23.35;
        public TankMonitorDialog()
        {
            InitializeComponent();
            InitChart();
			tanklist.Add("CO1P");
			tanklist.Add("NO1_COT_S");
			tanklist.Add("NO2_COT_P");
			tanklist.Add("NO2_COT_S");
			tanklist.Add("NO3_COT_P");
			tanklist.Add("NO3_COT_S");
			tanklist.Add("NO4_COT_P");
			tanklist.Add("NO4_COT_S");
			tanklist.Add("NO5_COT_P");
			tanklist.Add("NO5_COT_S");

			timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += TaskTimer_Tick;
            timer.Start();
        }


        private void TaskTimer_Tick(object sender, EventArgs e)
        {
			//Debug OPC取值待确认
			AddNo1PData(TankConstant.GetCurrentTankInn(tanklist[0]) / maxLevel * 100);
			AddNo1SData(TankConstant.GetCurrentTankInn(tanklist[1]) / maxLevel * 100);
			AddNo2PData(TankConstant.GetCurrentTankInn(tanklist[2]) / maxLevel * 100);
			AddNo2SData(TankConstant.GetCurrentTankInn(tanklist[3]) / maxLevel * 100);
			AddNo3PData(TankConstant.GetCurrentTankInn(tanklist[4]) / maxLevel * 100);
			AddNo3SData(TankConstant.GetCurrentTankInn(tanklist[5]) / maxLevel * 100);
			AddNo4PData(TankConstant.GetCurrentTankInn(tanklist[6]) / maxLevel * 100);
			AddNo4SData(TankConstant.GetCurrentTankInn(tanklist[7]) / maxLevel * 100);
			AddNo5PData(TankConstant.GetCurrentTankInn(tanklist[8]) / maxLevel * 100);
			AddNo5SData(TankConstant.GetCurrentTankInn(tanklist[9]) / maxLevel * 100);
		}


        private void InitChart()
        {
            // 舱室一（P）
            no1P.SetCurve("OPC", null, Colors.DarkOrange);
            // 舱室一（S）
            no1S.SetCurve("OPC", null, Colors.DarkOrange);


            // 舱室二（P）
            no2P.SetCurve("OPC", null, Colors.DarkOrange);
            // 舱室二（S）
            no2S.SetCurve("OPC", null, Colors.DarkOrange);


            // 舱室三（P）
            no3P.SetCurve("OPC", null, Colors.DarkOrange);
            // 舱室三（S）
            no3S.SetCurve("OPC", null, Colors.DarkOrange);


            // 舱室四（P）
            no4P.SetCurve("OPC", null, Colors.DarkOrange);
            // 舱室四（S）
            no4S.SetCurve("OPC", null, Colors.DarkOrange);


            // 舱室五（P）
            no5P.SetCurve("OPC", null, Colors.DarkOrange);
            // 舱室五（S）
            no5S.SetCurve("OPC", null, Colors.DarkOrange);
        }

        /// <summary>
        /// 添加舱室1P数据
        /// </summary>
        /// <param name="opc">OPC数据</param>
        public void AddNo1PData(double opc)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                no1P.AddCurveData(new string[] { "OPC" }, new double[] { opc });
            }));
        }

        /// <summary>
        /// 添加舱室1S数据
        /// </summary>
        /// <param name="opc">OPC数据</param>
        public void AddNo1SData(double opc)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                no1S.AddCurveData(new string[] { "OPC" }, new double[] { opc });
            }));
        }

        /// <summary>
        /// 添加舱室2P数据
        /// </summary>
        /// <param name="opc">OPC数据</param>
        public void AddNo2PData(double opc)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                no2P.AddCurveData(new string[] { "OPC" }, new double[] { opc });
            }));
        }

        /// <summary>
        /// 添加舱室2S数据
        /// </summary>
        /// <param name="opc">OPC数据</param>
        public void AddNo2SData(double opc)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                no2S.AddCurveData(new string[] { "OPC" }, new double[] { opc });
            }));
        }

        /// <summary>
        /// 添加舱室3P数据
        /// </summary>
        /// <param name="opc">OPC数据</param>
        public void AddNo3PData(double opc)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                no3P.AddCurveData(new string[] { "OPC" }, new double[] { opc });
            }));
        }

        /// <summary>
        /// 添加舱室3S数据
        /// </summary>
        /// <param name="opc">OPC数据</param>
        public void AddNo3SData(double opc)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                no3S.AddCurveData(new string[] { "OPC" }, new double[] { opc });
            }));
        }

        /// <summary>
        /// 添加舱室4P数据
        /// </summary>
        /// <param name="opc">OPC数据</param>
        public void AddNo4PData(double opc)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                no4P.AddCurveData(new string[] { "OPC" }, new double[] { opc });
            }));
        }

        /// <summary>
        /// 添加舱室4S数据
        /// </summary>
        /// <param name="opc">OPC数据</param>
        public void AddNo4SData(double opc)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                no4S.AddCurveData(new string[] { "OPC" }, new double[] { opc });
            }));
        }

        /// <summary>
        /// 添加舱室5P数据
        /// </summary>
        /// <param name="opc">OPC数据</param>
        public void AddNo5PData(double opc)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                no5P.AddCurveData(new string[] { "OPC" }, new double[] { opc });
            }));
        }

        /// <summary>
        /// 添加舱室5S数据
        /// </summary>
        /// <param name="opc">OPC数据</param>
        public void AddNo5SData(double opc)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                no5S.AddCurveData(new string[] { "OPC" }, new double[] { opc });
            }));
        }


        private void WindowMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


    }
}
